<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-dropdown>
          <el-button type="primary" style="margin: 0 10px;">
            批量操作<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>批量任务删除</el-dropdown-item>
            <el-dropdown-item>批量任务启动</el-dropdown-item>
            <el-dropdown-item>批量任务终止</el-dropdown-item>
            <el-dropdown-item>批量任务重试</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-input v-model="queryParams.searchValue" placeholder="请输入搜索内容" prefix-icon="el-icon-search" clearable
          style="width: 240px; margin-left: 10px;" @keyup.enter.native="handleQuery" />
        <el-button type="cyan" icon="el-icon-search" size="mini" style="width: 70px; margin-left: 10px;"
          @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="releaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="140" />
      <el-table-column label="平台" align="center" prop="platformType" width="140" />
      <el-table-column label="系列名" align="center" prop="seriesName" min-width="180" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布至" align="center" prop="releaseShopName" width="140" />

      <el-table-column label="总数" align="center" prop="totalCount" width="140" />
      <el-table-column label="成功数" align="center" prop="successCount" width="140" />
      <el-table-column label="失败数" align="center" prop="failCount" width="140" />
      <el-table-column label="任务状态" align="center" prop="taskStatus" width="140" :formatter="taskStatusFormat" />
      <el-table-column label="任务进度" align="center" prop="productType" width="140" />
      <el-table-column label="消息" align="center" prop="message" width="140" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300">
        <template slot-scope="scope">
          <el-button type="success" plain @click="handleView(scope.row)"
            v-hasPermi="['release:dataRelease:queryReleaseDetail']">查看发布记录</el-button>
          <el-dropdown @command="(data) => handleCommand(data, scope.row.id)">
            <el-button type="primary" style="margin: 0 10px;">
              更多操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="start">启动任务</el-dropdown-item>
              <el-dropdown-item command="stop">终止任务</el-dropdown-item>
              <el-dropdown-item command="delete">删除任务</el-dropdown-item>
              <el-dropdown-item command="modify">修改参数</el-dropdown-item>
              <el-dropdown-item command="reset">重置任务</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

          <!-- <el-button type="danger"  plain @click="handleDelete(scope.row)"
            v-hasPermi="['product:productInfo:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pageSize" @pagination="getReleaseList" />

    <detailsDialog v-if="detailsStatus" :releaseId="releaseId" :dialogTitle="dialogTitle"
      @closeDialog="detailsStatus = false" />
  </div>
</template>

<script>
import {
  queryReleaseList,
  queryReleaseDetail,
  deleteData,
} from "@/api/dataRelease/release";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import detailsDialog from "./components/detailsDialog.vue";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";


export default {
  name: "release",
  components: { Treeselect, detailsDialog },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发布记录表格数据
      releaseList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 产品类型数据字典
      taskStatusOptions: [],
      // 表单参数
      form: {
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageSize: 10
      },
      show: false,
      detailsStatus: false,
      releaseId: '',
    };
  },

  created() {
    this.getReleaseList();
    this.getDicts("task_status").then((response) => {
      this.taskStatusOptions = response.data;
    });
  },
  methods: {
    // 下拉触发事件
    handleCommand(type, id) {
      console.log('type', type);
      switch (type) {
        case 'delete':
          this.deleteTask(id)
          break;

        default:
          break;
      }
    },
    // 删除任务
    deleteTask(id) {
      this.$confirm(
        '是否确认删除发布记录编码为"' + id + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteData({ id }).then(res => {
            this.getReleaseList();
            this.msgSuccess("删除成功");
          })
        })

    },
    // 查看发布记录
    handleView(query) {
      this.dialogTitle = `任务编号 ${query.id} 的发布信息`
      this.releaseId = query.id
      this.detailsStatus = true
    },
    /** 查询产品列表 */
    getReleaseList() {
      this.loading = true;
      console.log(123)
      queryReleaseList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {

          this.releaseList = response.value.list;
          this.total = response.value.total;
          this.loading = false;
        }
      );
    },

    // 产品类型字典翻译
    taskStatusFormat(row, column) {
      return this.selectDictLabel(this.taskStatusOptions, row.taskStatus);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productNum: undefined,
        productType: undefined,
        tradeFace: undefined,
        state: '0',
        unit: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.currentPage = 1;
      this.getReleaseList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const releaseIds = row.id || this.ids;
      this.$confirm(
        '是否确认删除发布记录编码为"' + releaseIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return deleteData(releaseIds);
        })
        .then(() => {
          this.getReleaseList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },
  },
};
</script>